/* GUI CONVERTED from recloseb.p (converter v1.76) Wed Apr 10 12:21:35 2002 */
/* recloseb.p - REPETITIVE                                                    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.8.1.16 $                                                      */
/*V8:ConvertMode=Report                                                       */
/* REVISION: 7.3      LAST MODIFIED: 10/31/94   BY: WUG *GN77*                */
/* REVISION: 7.3      LAST MODIFIED: 11/08/94   BY: WUG *GN98*                */
/* REVISION: 7.3      LAST MODIFIED: 03/03/95   BY: WUG *G0GJ*                */
/* REVISION: 7.3      LAST MODIFIED: 03/31/95   BY: WUG *G0K0*                */
/* REVISION: 8.5      LAST MODIFIED: 12/10/95   BY: sxb *J053*                */
/* REVISION: 8.5      LAST MODIFIED: 03/13/96   BY: JYM *G1GD*                */
/* REVISION: 8.5      LAST MODIFIED: 06/06/96   BY: JYM *G1XF*                */
/* REVISION: 8.5      LAST MODIFIED: 07/29/97   BY: *G2P0* Felcy D'Souza      */
/* REVISION: 8.5      LAST MODIFIED: 08/08/97   BY: *H1CZ* Felcy D'Souza      */

/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 08/04/99   BY: *N014* Jeff Wootton       */
/* REVISION: 9.1      LAST MODIFIED: 11/17/99   BY: *N04H* Vivek Gogte        */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 06/02/00   BY: *N09M* Inna Lyubarsky     */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* myb                */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Revision: 1.8.1.7       BY: Pat Pigatti      DATE: 09/22/00  ECO: *N0S7*   */
/* Revision: 1.8.1.11      BY: Mark Christian   DATE: 11/10/00  ECO: *M0W5*   */
/* Revision: 1.8.1.13      BY: Niranjan R.      DATE: 07/13/01  ECO: *P00L*   */
/* Revision: 1.8.1.14      BY: Mark Christian   DATE: 10/05/01  ECO: *N13H*   */
/* $Revision: 1.8.1.16 $   BY: Katie Hilbert    DATE: 02/25/02  ECO: *N194*   */

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

         /* CUM WORKORDER ACCTG CLOSE/WIP TRANSFER SUBPROGRAM */
         /* POST OLD ORDER WIP TO SCRAP                       */


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

define input parameter wolot as character.
define input parameter eff_date as date.
define input parameter transfer_wip like mfc_logical.

define new shared variable op_recno   as recid.
define new shared variable opgltype   like opgl_type.
define new shared variable ref        like glt_ref.
define new shared variable sf_cr_acct like dpt_lbr_acct.
define new shared variable sf_cr_sub  like dpt_lbr_sub.
define new shared variable sf_cr_cc   like dpt_lbr_cc.
define new shared variable sf_dr_acct like dpt_lbr_acct.
define new shared variable sf_dr_sub  like dpt_lbr_sub.
define new shared variable sf_dr_cc   like dpt_lbr_cc.
define new shared variable sf_entity  like en_entity.
define new shared variable sf_gl_amt  like tr_gl_amt.

define shared variable warn_ct as integer no-undo.

define shared workfile work_op_recids no-undo field work_op_recid
   as recid.

define variable dept as character.
define variable emp as character.
define variable mthd_chg_var like glt_amt
   column-label "Method Chg!Variance".
define variable prev_op_cost like iro_cost_tot
   column-label "Prev Op!Cost Total".
define variable qty_inque as decimal.

define variable clear_acct    as character.
define variable clear_sub     as character.
define variable clear_cc      as character.
define variable mthd_chg_acct as character no-undo.
define variable mthd_chg_sub  as character no-undo.
define variable mthd_chg_cc   as character no-undo.
define variable shift         as character.
define variable trans_type    as character.
define variable wip_qty_value like tr_gl_amt
   column-label "Extended Cost".
define variable glx_set like cs_set.
define variable glx_mthd like cs_method.
define variable cur_set like cs_set.
define variable cur_mthd like cs_method.
define variable gl_cost_set as character.
define variable wip_gl_amt as decimal.
define variable ophist_recid as recid.
define variable sct_recid as recid.

define variable op_mtl     as decimal no-undo.
define variable op_lbr     as decimal no-undo.
define variable op_bdn     as decimal no-undo.
define variable op_sub     as decimal no-undo.
define variable op_mtl_tot as decimal no-undo.
define variable op_lbr_tot as decimal no-undo.
define variable op_bdn_tot as decimal no-undo.
define variable op_sub_tot as decimal no-undo.
define variable qty_oh     like in_qty_oh no-undo.
define variable oldcst     like sct_cst_tot no-undo.
define variable glamt      as decimal no-undo.
define variable curflag    like mfc_logical no-undo.
define variable glxflag    like mfc_logical no-undo.
define variable qty_wo     like wo_qty_comp no-undo.
define variable l_ord_nbr  as character no-undo.
define variable rndmthd    like gl_rnd_mthd no-undo.

FORM /*GUI*/ 
   wr_op
   wr_qty_inque
   prev_op_cost
   wr_qty_outque
   wr_qty_rejque
   iro_cost_tot
   sf_gl_amt
with STREAM-IO /*GUI*/  width 132 down
title color normal
getFrameTitle("WIP_TRANSFERRED_TO_CLEARING", 29)
frame b.

/* SET EXTERNAL LABELS */
setFrameLabels(frame b:handle).

find wo_mstr where wo_lot = wolot exclusive-lock.
find si_mstr where si_site = wo_site no-lock.
sf_entity = si_entity.
for first gl_ctrl
   fields(gl_rnd_mthd gl_wvar_acct gl_wvar_sub gl_wvar_cc)
no-lock: end.
if available gl_ctrl then rndmthd = gl_rnd_mthd.

for first icc_ctrl
   fields(icc_cogs icc_gl_sum icc_gl_tran icc_gl_set
          icc_cur_set icc_mirror icc_jrnl
          icc_xclr_acct icc_xclr_sub icc_xclr_cc)
no-lock: end.

/*DETERMINE COSTING METHOD*/
{gprun.i ""csavg01.p"" "(input wo_part,
                         input wo_site,
                         output glx_set,
                         output glx_mthd,
                         output cur_set,
                         output cur_mthd)" }

/* IF NOT TRANSFER_WIP AND CURRENT COST SET OR GL COST = AVERAGE THEN  */
/* CALCULATE OPERATIONAL COST AND DO AVERAGE COSTING                   */

if not transfer_wip and
  (glx_mthd = "AVG" or cur_mthd = "AVG"
   or cur_mthd = "LAST")
then do:

   assign
      op_lbr_tot = 0
      op_bdn_tot = 0
      op_sub_tot = 0
      op_mtl_tot = 0
      op_mtl     = 0
      op_lbr     = 0
      op_bdn     = 0
      op_sub     = 0.

   for first in_mstr
      fields (in_part in_site in_qty_oh in_qty_nonet
              in_gl_set in_cur_set)
       where in_part = wo_part
         and in_site = wo_site
   no-lock: end.

   for each wr_route where wr_lot = wolot no-lock
         break by wr_op:

      /*op_lbr_tot, op_bdn_tot, op_sub_tot & op_mtl_tot         */
      /*USED FOR LAST COST ONLY.                                */
      assign
         op_lbr_tot = op_lbr_tot + wr_lbr_act
         op_bdn_tot = op_bdn_tot + wr_bdn_act
         op_sub_tot = op_sub_tot + wr_sub_act
         op_lbr = op_lbr + wr_lbr_act - wr_lbr_totx
         op_bdn = op_bdn + wr_bdn_act - wr_bdn_totx
         op_sub = op_sub + wr_sub_act - wr_sub_totx

         /* qty_wo IS BEING INITIALISED AT EACH OPERATION. */
         qty_wo = wr_qty_cummove + wr_qty_rejqueb

         /*wr_mtl_ll_act IS  THE SUMMATION OF THE COST OF MATERIAL ADDED   */
         /*IN ALL THE PREVIOUS AND ALSO THIS OPERATION FOR THE QUANTITY    */
         /*COMPLETED IN THIS OPERATION.                                    */
         /*wr_mtl_ll_totx is IS THE SUMMATION OF COST OF MATERIAL ADDED AT */
         /*ALL PREVIOUS OPERATION AND THIS OPERATION FOR THE QTY COMPLETED */
         /*IN THE NEXT OPEARTION.                                          */
         /*wr_mtl_ll_act - wr_mtl_ll_totx IS THE SUMMATION OF THE RESIDUAL */
         /*WIP VALUE AT THIS OPERATION.                                    */

         op_mtl = op_mtl + wr_mtl_ll_act - wr_mtl_ll_totx.

   end. /* for each wr_route */

   for each wod_det where wod_lot = wolot no-lock:
      assign
         /*USED FOR LAST COST ONLY*/
         op_mtl_tot = op_mtl_tot + wod_tot_std.
   end. /* for each wod_det */

   assign
      qty_oh = in_qty_oh + in_qty_nonet.

   {gpsct04.i &type=""CUR""}

   if available sct_det
      and available in_mstr
      and (in_qty_oh + in_qty_nonet > 0)
      and qty_wo <> 0
      and (op_mtl <> 0 or op_lbr <> 0 or op_bdn <> 0 or op_sub <> 0)
      and (wo_qty_ord > 0)
   then do:
      assign
         sct_recid = recid(sct_det)
         curflag = yes.
      for first sct_det
         where recid(sct_det) = sct_recid exclusive-lock:
      end.
      if available sct_det
      then do:
         if cur_mthd = "AVG" then
            assign
               sct_mtl_ll = max(0, ((sct_mtl_ll * qty_oh) + op_mtl) / qty_oh)
               sct_lbr_tl = max(0, ((sct_lbr_tl * qty_oh) + op_lbr) / qty_oh)
               sct_bdn_tl = max(0, ((sct_bdn_tl * qty_oh) + op_bdn) / qty_oh)
               sct_sub_tl = max(0, ((sct_sub_tl * qty_oh) + op_sub) / qty_oh).
         else
         if cur_mthd = "LAST" then
            assign
               sct_mtl_ll = max(0, (op_mtl_tot / qty_wo))
               sct_lbr_tl = max(0, (op_lbr_tot / qty_wo))
               sct_bdn_tl = max(0, (op_bdn_tot / qty_wo))
               sct_sub_tl = max(0, (op_sub_tot / qty_wo)).

         assign
            sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl + sct_ovh_tl
                        + sct_mtl_ll + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                        + sct_sub_ll + sct_sub_tl
            sct_cst_date = today.
      end. /* avail sct_det */
   end. /* avail sct_det & in_mstr, ect for current */

   if glx_mthd = "AVG" then do:

      {gpsct04.i &type=""GL""}

      if available sct_det
         and available in_mstr
         and (in_qty_oh + in_qty_nonet > 0)
         and (op_mtl <> 0 or op_lbr <> 0 or op_bdn <> 0 or op_sub <> 0)
         and (wo_qty_ord > 0)
      then do:
         sct_recid = recid(sct_det).
         for first sct_det
            where recid(sct_det) = sct_recid exclusive-lock:
         end.
         if available sct_det
         then do:
            assign
               glxflag = yes
               oldcst = sct_cst_tot
               sct_mtl_ll = max(0, ((sct_mtl_ll * qty_oh) + op_mtl) / qty_oh)
               sct_lbr_tl = max(0, ((sct_lbr_tl * qty_oh) + op_lbr) / qty_oh)
               sct_bdn_tl = max(0, ((sct_bdn_tl * qty_oh) + op_bdn) / qty_oh)
               sct_sub_tl = max(0, ((sct_sub_tl * qty_oh) + op_sub) / qty_oh)
               sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl +
                             sct_ovh_tl + sct_mtl_ll + sct_lbr_ll +
                             sct_bdn_ll + sct_ovh_ll + sct_sub_ll +
                             sct_sub_tl
               sct_cst_date = today
               wo_wip_tot = wo_wip_tot - ((sct_cst_tot - oldcst) * qty_oh).

            if sct_cst_tot <> oldcst then do:

               /* UPDATE TRANSACTION HISTORY RECORD */
               /* 5206 - WO ACCT CLOSE: Average in  */
               /*        costs posted after final WO*/
               /*        receipt                    */

               glamt = (sct_cst_tot - oldcst) * qty_oh.

               /* iccstinv.p IS NOW USED TO UPDATE THE TRANSACTION HISTORY RECORD */
               /* INSTEAD OF recrtr.p BECAUSE THE FORMER MAKES ONE ADDITIONAL     */
               /* SEARCH ON THE pld_det TABLE, NAMELY A SEARCH USING PARAMETERS   */
               /* PRODUCT LINE + SITE + LOCATION, WHERE THE LOCATION IS NOT BLANK */

               for first pt_mstr
                  fields (pt_abc pt_avg_int pt_cyc_int pt_part
                          pt_prod_line pt_site pt_um)
                  where pt_part = wo_part
               no-lock:
               end. /* FOR FIRST pt_mstr */

               assign
                  trmsg     = 5206
                  l_ord_nbr = "CSAVG02" + wo_nbr.

               {gprun.i ""iccstinv.p""
                  "(input oldcst,
                    buffer sct_det,
                    input l_ord_nbr,
                    input wo_site,
                    input icc_cogs,
                    input icc_gl_sum,
                    input icc_gl_tran,
                    input icc_mirror,
                    input icc_jrnl,
                    input rndmthd,
                    input pt_part,
                    input pt_prod_line,
                    input pt_site,
                    input pt_um)"}

            end. /* if sct_cst_tot <> oldcst - create cost adj record */
         end. /* avail sct_det */
      end. /* avail sct_det & in_mstr, ect for current */
   end. /* if glx_mthd = "AVG" */

   /* * * RUN csavg2b.p SO THAT THE COST SIMULATION TOTAL DETAIL           */
   /*          MATCHES THE COST SIMULATION ITEM DETAIL                     */
   {gprun.i ""csavg2b.p"" "(input wo_part,
                            input wo_site,
                            input cur_set,
                            input curflag,
                            input glx_set,
                            input glxflag)"
      }.

end. /* transfer_wip = no */

if glx_mthd = "avg" then
   gl_cost_set = "cumorderavg".
else
   gl_cost_set = "cumorder".

/*GET DEFAULT CLEARING ACCOUNT*/
assign
   clear_acct = ""
   clear_sub  = ""
   clear_cc   = "".

for first rpc_ctrl
   fields(rpc_wxfer_acct rpc_wxfer_sub rpc_wxfer_cc)
no-lock:
end.
if available rpc_ctrl
   and rpc_wxfer_acct <> ""
then
   assign
      clear_acct = rpc_wxfer_acct
      clear_sub  = rpc_wxfer_sub
      clear_cc   = rpc_wxfer_cc.
else do:
   if available icc_ctrl and icc_xclr_acct > "" then
      assign
         clear_acct = icc_xclr_acct
         clear_sub  = icc_xclr_sub
         clear_cc   = icc_xclr_cc.
end. /* NOT AVAIL rpc_ctrl */

if not available pt_mstr then
   for first pt_mstr
      fields (pt_part pt_prod_line)
      where pt_part = wo_part
   no-lock:
end. /* FOR FIRST pt_mstr */
find pl_mstr where pl_prod_line = pt_prod_line no-lock.

if clear_acct = "" then do:
   if pl_wvar_acct > ""
   then do:
      {gprun.i ""glactdft.p"" "(input ""WO_WVAR_ACCT"",
                                input pt_prod_line,
                                input wo_site,
                                input """",
                                input """",
                                input no,
                                output clear_acct,
                                output clear_sub,
                                output clear_cc)"}
   end.  /* IF PL_WVAR_ACCT */
   else
      assign
         clear_acct = gl_wvar_acct
         clear_sub  = gl_wvar_sub
         clear_cc   = gl_wvar_cc.

end. /* clear_acct = "" */

/* GET THE METHOD CHANGE ACCOUNT*/
if pl_wvar_acct > "" then
   assign
      mthd_chg_acct = clear_acct
      mthd_chg_sub  = clear_sub
      mthd_chg_cc   = clear_cc.
else
   assign
      mthd_chg_acct = gl_wvar_acct
      mthd_chg_sub  = gl_wvar_sub
      mthd_chg_cc   = gl_wvar_cc.

prev_op_cost = 0.

for each wr_route exclusive-lock
   where wr_lot = wo_lot
   break by wr_op:

   dept = "".
   find wc_mstr where wc_wkctr = wr_wkctr and wc_mch = wr_mch
      no-lock no-error.

   if available wc_mstr then dept = wc_dept.

   trans_type = "CLOSE".

   {gprun.i ""reophist.p"" "(input trans_type,
                             input wr_lot,
                             input wr_op,
                             input emp,
                             input wr_wkctr,
                             input wr_mch,
                             input dept,
                             input shift,
                             input eff_date,
                             output ophist_recid)"}

   create work_op_recids.
   work_op_recid = ophist_recid.

   if glx_mthd = "AVG" then
      wip_gl_amt = (wr_lbr_ll_act - wr_lbr_ll_totx) +
                   (wr_bdn_ll_act - wr_bdn_ll_totx) +
                   (wr_sub_ll_act - wr_sub_ll_totx) +
                   (wr_mtl_ll_act - wr_mtl_ll_totx) +
                   (wr_lbr_act - wr_lbr_totx) +
                   (wr_bdn_act - wr_bdn_totx) +
                   (wr_sub_act - wr_sub_totx) +
                   (wr_mtl_act - wr_mtl_totx).
   else do:
      find iro_det
         where iro_part = wo_part
           and iro_site = wo_site
           and iro_cost_set = gl_cost_set
           and iro_routing = wo_lot
           and iro_op = wr_op
         no-lock.

      /*NOTE: INQUEUE QTY AT FIRST OP IS MEANINGLESS*/
      if first(wr_op) then
         qty_inque = 0.
      else
         qty_inque = wr_qty_inque.

      assign
         wip_qty_value = (qty_inque * prev_op_cost)
                       + (wr_qty_outque + wr_qty_rejque)
                       * iro_cost_tot
         wip_gl_amt = wip_qty_value.
   end.
   /* ROUND TRANSFERRED COST TO BASE CURRENCY PRECISION */
   {gprun.i ""gpcurrnd.p"" "(input-output wip_gl_amt,
                             input rndmthd)"}

   /*WHEN TRANSFER WIP = YES, WIP IS FIRST MOVED TO THE TRANSFER    */
   /*CLEARING ACCOUNT THEN TO THE NEW ORDER AND A METHOD CHANGE     */
   /*VARIANCE WILL TAKE PLACE IF ANY COSTS HAVE CHANGED FROM THE OLD*/
   /*TO THE NEW ORDER.                                              */
   /*WHEN TRANSFER WIP = NO, ANY OUTSTANDING RESIDUAL WIP COSTS ON  */
   /*A CUMULATIVE ORDER ARE TRANSFERRED TO THE METHOD CHANGE        */
   /*VARIANCE ACCOUNT.                                              */

   if transfer_wip then
      assign
         sf_dr_acct = clear_acct
         sf_dr_sub  = clear_sub
         sf_dr_cc   = clear_cc.
   else
      assign
         sf_dr_acct = mthd_chg_acct
         sf_dr_sub  = mthd_chg_sub
         sf_dr_cc   = mthd_chg_cc.

   assign
      sf_cr_acct = wo_acct
      sf_cr_sub  = wo_sub
      sf_cr_cc   = wo_cc
      sf_gl_amt  = wip_gl_amt
      opgltype   = trans_type
      op_recno   = ophist_recid.

   {gprun.i ""sfopgl.p""}

   wr_status = "c".

   display
      wr_op
      wr_qty_inque         when (not first(wr_op))
      prev_op_cost         when (glx_mthd <> "AVG" and not first(wr_op))
      wr_qty_outque
      wr_qty_rejque
      iro_cost_tot         when (glx_mthd <> "AVG")
      sf_gl_amt(total)
   with frame b STREAM-IO /*GUI*/ .

   wo_wip_tot = wo_wip_tot - wip_gl_amt.

   if glx_mthd <> "AVG" then
      prev_op_cost = iro_cost_tot.
end.
/* IF ANY REMAINING WIP VALUE,                 */
/* THEN POST TO METHOD CHANGE VARIANCE ACCOUNT */
if wo_wip_tot <> 0 then do:
   assign
      sf_dr_acct = mthd_chg_acct
      sf_dr_sub  = mthd_chg_sub
      sf_dr_cc   = mthd_chg_cc
      sf_cr_acct = wo_acct
      sf_cr_sub  = wo_sub
      sf_cr_cc   = wo_cc
      sf_gl_amt  = wo_wip_tot
      opgltype   = trans_type.
   op_recno = ophist_recid.
   {gprun.i ""sfopgl.p""}
   wo_wip_tot = 0.
end. /* wo_wip_tot <> 0 */

wo_status = "c".
